/*
 * @Author: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @Date: 2024-05-11 14:18:39
 * @LastEditors: error: error: git config user.name & please set dead value or install git && error: git config user.email & please set dead value or install git & please set dead value or install git
 * @LastEditTime: 2024-05-24 10:16:59
 * @FilePath: \driving-training-admin\src\views\login\index.ts
 * @Description: 这是默认设置,请设置`customMade`, 打开koroFileHeader查看配置 进行设置: https://github.com/OBKoro1/koro1FileHeader/wiki/%E9%85%8D%E7%BD%AE
 */
import { getCurrentUserInfo } from '@/apis/login'
import { storage } from '@/store/sessionStorage'
import { userStore } from '@/store/user.store'
import { checkPassword_2 } from '@/utils'
import { ElMessage, FormInstance } from 'element-plus'
import { reactive, ref } from 'vue'
import { useRouter } from 'vue-router'

export default class Login {
    account = reactive({
        name: '',
        password: ''
    })
    uStore = userStore()
    router = useRouter()
    rules = reactive({
        name: [{ required: true, message: '请输入用户名', trigger: 'blur' }],
        password: [{ required: true, message: '请输入密码', trigger: 'blur' }]
    })
    rememberPassword = ref(false)
    startLogin = false
    constructor() {
        this.uStore.setToken('')
    }

    submitForm = async (formEl: FormInstance | undefined) => {
        if (!formEl) return
        await formEl.validate((valid, fields) => {
            if (valid) {
                console.log('submit!', this.startLogin)
                if (this.startLogin) {
                    return false
                }
                this.startLogin = true
                if (this.rememberPassword.value) {
                    localStorage.setItem('rememberPassword', 'true')
                }
                this.uStore.login(this.account.name, this.account.password).then(
                    () => {
                        this.startLogin = true

                        //获取当前用户信息
                        getCurrentUserInfo().then(res => {
                            if (Number(res.data.code) === 200) {
                                storage.set('userInfo', JSON.stringify(res.data.result.userBasic))
                                this.uStore.name = res.data.result.userBasic.name
                                this.uStore.account = res.data.result.userBasic.account
                                console.log('res', this.uStore)
                                if (res.data.result.apiUrls.length) {
                                    ElMessage({
                                        type: 'success',
                                        message: '欢迎登录系统'
                                    })

                                    this.router.push('/index')
                                } else {
                                    storage.clear()
                                    ElMessage({
                                        type: 'error',
                                        message: '您没有权限访问该系统'
                                    })
                                }
                            }
                        })
                    },
                    (err: any) => {
                        this.startLogin = false
                        console.log('err', err, this.startLogin)
                        // ElMessage.error(err.message)
                    }
                )
            } else {
                console.log('error submit!', fields)
            }
        })
    }
}
